<%@page language="java" contentType="text/javascript; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import='java.sql.*' %>
<%@page import="java.util.ArrayList"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="net.sf.json.JSONSerializer"%>
<%@page import="net.sf.json.JSONArray"%>


<%String callback = request.getParameter("callback");%>
<%=callback %> (<%

	String id = request.getParameter("id");
	String name = new String(request.getParameter("name").getBytes("8859_1"), "utf-8");
	String email = request.getParameter("email");
	String age = request.getParameter("age");
	String gender = request.getParameter("gender");
	//String userlat = request.getParameter("userlat");
	//String userlng = request.getParameter("userlng");

		
	Connection conn = null;
    PreparedStatement pstmt = null;
    
    try {
		Class.forName("com.mysql.jdbc.Driver");
		conn = DriverManager.getConnection( 	
			"jdbc:mysql://localhost/itscreater", "itscreater", "campus123");
		
		String select = "select * from UserInfo where ID = ?;";
		pstmt = conn.prepareStatement(select);
		pstmt.setString(1, id);
		ResultSet rs = pstmt.executeQuery();
		JSONObject jo = new JSONObject();
		
		if(rs.next()) {
			String update = "update UserInfo set Name:= ?, Email:= ?," +
							"Age= ?, Gender= ? where ID = ?;";
			pstmt = conn.prepareStatement(update);
			pstmt.setString(1, name);
			pstmt.setString(2, email);
			pstmt.setString(3, age);
			pstmt.setString(4, gender);
			pstmt.setString(5, id);
			//pstmt.setString(6, userlat);
			//pstmt.setString(7, userlng);
			pstmt.executeUpdate();
			
			pstmt = conn.prepareStatement(select);
			pstmt.setString(1, id);
			rs = pstmt.executeQuery();
			rs.next();
			
			String getname = rs.getString("Name");
			String getemail = rs.getString("Email");
			String getage = rs.getString("Age");
			String getgender = rs.getString("Gender");
			String getintroduction = rs.getString("Introduction");
			if (getintroduction != null) {
				getintroduction = getintroduction.replace("\n","<br>");
				getintroduction = getintroduction.replace("\u0020","&nbsp;");
			}
			String getcacao = rs.getString("Cacao");
			
			jo.put("ID", id);
			jo.put("Name", getname);
			jo.put("Email", getemail);
			jo.put("Age", getage);
			jo.put("Gender", getgender);
			jo.put("Introduction", getintroduction);
			jo.put("Cacao", getcacao);
			
			jo.put("success", "true");
			jo.put("message", "Good");
		} else {
			jo.put("success", "true");
			jo.put("message", "Bad");
			
		}
		
			
		out.print(jo);
	
		

	}
    	
    	finally {
        try { 
            pstmt.close();
        } 
        catch (Exception ignored) {
        }
        try { 
            conn.close();
        } 
        catch (Exception ignored) {
        }
    }
   

%>);

